package com.cyys.modules.system.tag;

import com.cyys.common.utils.StringUtil;
import com.cyys.modules.core.shiro.token.manage.TokenManager;
import com.cyys.modules.system.model.user.Menu;
import com.cyys.modules.system.model.user.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.List;

public class LeftMenuTag extends SimpleTagSupport {

	@Override
	public void doTag() throws JspException, IOException {
		StringBuffer menu = new StringBuffer();
		String contextPath = ((HttpServletRequest) ((PageContext) this.getJspContext()).getRequest()).getContextPath();
		User user = TokenManager.getToken();
		List<Menu> menuList = user.getMenuListTree();
		
		menu.append("<ul class='layui-nav layui-nav-tree' lay-shrink='all' id='LAY-system-side-menu' lay-filter='layadmin-system-side-menu'>");

		// 树形菜单
		if (menuList != null && menuList.size() > 0) {
			for (Menu treeNode : menuList) {
				if (treeNode.getIsShow().equals("1")) { // 菜单是否显示
					menu.append("<li data-name='");
					menu.append(treeNode.getShiro().replaceAll(":",""));
					menu.append("' class='layui-nav-item'>");

					menu.append("<a href='javascript:;' lay-tips='>").append(treeNode.getName()).append("'");
					if (StringUtil.isNotBlank(treeNode.getHref())) {
						menu.append(" lay-href='>").append(treeNode.getHref()).append("'");
					}
					menu.append(" lay-direction='2'>");

					if(StringUtil.isNotBlank(treeNode.getIcon())){
						menu.append("<i class='layui-icon ").append(treeNode.getIcon()).append("'></i>");
					}
					menu.append("<cite>");
					menu.append(treeNode.getName());
					menu.append("</cite></a>");
					//组装子菜单
					setChildren(contextPath, menu, treeNode, 2);
					menu.append("</li>");
				}
			}
		}
		menu.append("</ul>");
		getJspContext().getOut().write(menu.toString());
	}

	public void setChildren(String contextPath, StringBuffer menu, Menu treeNode, int level){
		if(level>3){//目前只支持3级
			return;
		}
		String tag1= (level== 2?"dl":"ol");
		String tag2= (level== 2?"dd":"li");
		// 是否存在子菜单
		if (treeNode.getChildren() != null && treeNode.getChildren().size() > 0) {
			menu.append("<").append(tag1).append(" class='layui-nav-child'>");
			for (Object obj : treeNode.getChildren()) {
				Menu treeNodeChild = (Menu) obj;
				if (treeNodeChild.getIsShow().equals("1")) {   //判断是否显示
					menu.append("<").append(tag2).append(" data-name='button");
					menu.append(treeNodeChild.getShiro().replaceAll(":",""));
					menu.append("'>");

					//判断是否有链接
					if (treeNodeChild.getHref() != null && !treeNodeChild.getHref().equals("")) {
						menu.append("<a lay-href='");
						menu.append(contextPath);
						menu.append(treeNodeChild.getHref());
						menu.append("'>");
					}else{
						menu.append("<a>");
					}
					if(StringUtil.isNotBlank(treeNodeChild.getIcon())){
						menu.append("<i class='layui-icon ").append(treeNodeChild.getIcon()).append("'></i>");
					}
					menu.append(treeNodeChild.getName());
					menu.append("</a>");
					setChildren(contextPath, menu, treeNodeChild, level+1);
					menu.append("</").append(tag2).append(">");
				}
			}
			menu.append("</").append(tag1).append(">");
		}
	}
}
